﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Models;
using QLBH.Modules;
using QLBH.Utilities;

namespace QLBH.Controllers
{
    class BranchsController
    {
        private ucBranchs ucBranchs;
        private BranchsModel objModel;
        public BranchsController(ucBranchs ucBranchs)
        {
            objModel = new BranchsModel();
            this.ucBranchs = ucBranchs;
        }

        internal void loadData()
        {
            try
            {
                List<BranchsModel> data = WSAccess.GetData<List<BranchsModel>>(MainForm.Instance.Shop_id.ToString() + "/branchs");
                ucBranchs.DisplayData(data);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
      
        internal void Add()
        {
            frmAdd_Edit_Branchs frmBranchs = new frmAdd_Edit_Branchs(eType.ADD);
            frmBranchs.Text = "Đăng ký chi nhánh";
            if (frmBranchs.ShowDialog() == DialogResult.OK)
            {
                frmBranchs.Close();
                loadData();
            }
        }
        internal void Edit()
        {
            frmAdd_Edit_Branchs frmBranchs = new frmAdd_Edit_Branchs(eType.EDIT);
            frmBranchs.Text = "Sửa chi nhánh";
            try
            {
                if (ucBranchs.SelectItem() != null)
                {
                    frmBranchs.objModel = ucBranchs.SelectItem();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (frmBranchs.ShowDialog() == DialogResult.OK)
            {
                loadData();
            }
        }
        internal void Delete()
        {
            if (ucBranchs.SelectItem() != null)
            {
                objModel = ucBranchs.SelectItem();
                if (MessageBox.Show("Bạn muốn xóa chi nhánh?", "Xóa", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
                {
                    try
                    {
                        WSAccess.Delete<BranchsModel>(objModel.Id + "branch/delete", objModel);
                        loadData();
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn dữ liệu trước khi xóa");
            }


        }

    }
}
